﻿using JamesZhao.Engine.GameObjects;
using SlimDX.DirectInput;

namespace JamesZhao.Engine.GameComponents
{
    public abstract class GameComponent
    {
        private GameObject _owner;

        protected GameComponent(GameObject owner)
        {
            Owner = owner;
        }

        public GameObject Owner
        {
            get { return _owner; }
            set
            {
                if (_owner != null)
                {
                    _owner.RemoveComponent(this);
                }
                _owner = value;
                _owner.AddComponent(this);
                OnOwnerChanged();
            }
        }

        public GameEngine Engine
        {
            get { return Owner.Engine; }
        }

        public InputManager Input
        {
            get { return Engine.InputManager; }
        }

        public TransformComponent Transform
        {
            get { return _owner.Transform; }
        }

        public virtual void Initialize()
        {
        }

        public virtual void Update()
        {
        }

        public virtual void Destroy()
        {
        }

        public virtual void OnOwnerChanged()
        {
        }

        protected bool GetKey(Key key)
        {
            return Owner.Engine.InputManager.GetKey(key);
        }
    }
}